package com.scedu.service.impl;

import com.lly835.bestpay.config.WxPayConfig;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.scedu.dto.OrderDTO;
import com.scedu.enums.ResultEnums;
import com.scedu.exception.SellException;
import com.scedu.service.PayService;
import com.scedu.utils.JsonUtil;
import com.scedu.utils.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service //业务层
@Slf4j
public class PayServiceImpl implements PayService {
    @Autowired
    private BestPayServiceImpl bestPayService;
    @Autowired
    private OrderServiceImpl orderService;
    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_MP);//支付方式 WXPAY_MWEB=微信H5
        payRequest.setOrderId(orderDTO.getOrderId());//订单编号
        payRequest.setOrderName("微信公众账号支付订单");//订单名称
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());//订单金额
       // payRequest.setOpenid(orderDTO.getOrderOpenid());//订单用户openid
        payRequest.setOpenid("oTgZpwXONyc3K25NjheiDFLNqkX0");//大师兄订单用户openid
        log.info("订单发起支付信息，payRequest={}", JsonUtil.ToJson(payRequest));
        PayResponse payResponse=bestPayService.pay(payRequest);
        log.info("订单发起支付response，payResponse={}",JsonUtil.ToJson(payResponse));
        return payResponse;

    }

    @Override
    public PayResponse notify(String notifyData) {
        //处理回调信息
        //1.验证签名
        //2.支付状态
        //3.支付的金额检验
        //4.支付人核实
        log.info("微信支付异步通知notify notifynotifynotify");
        log.info("微信支付异步通知00，notifyData={}",notifyData);
        //1.接收异步通知
        PayResponse payResponse=bestPayService.asyncNotify(notifyData);
        log.info("微信支付异步通知，payResponse={}",JsonUtil.ToJson(payResponse));
        //2.修改订单的支付状态
        //2.1查询订单
        OrderDTO orderDTO=orderService.findOne(payResponse.getOrderId());
        //2.2判断订单是否存在
        if (orderDTO==null){
            log.error("微信支付异步通知,订单不存在，orderId={}",payResponse.getOrderId());
            throw  new SellException(ResultEnums.ORDER_NOT_EXIT_ERROR);

        }
        //2.3判断金额是否一致 (orderDTO.getOrderAmount() payResponse.getOrderAmount()
        if(!MathUtil.equals(orderDTO.getOrderAmount().doubleValue(),payResponse.getOrderAmount())){
            log.error("微信支付异步通知,金额不一致，orderId={}",payResponse.getOrderId());
            throw  new SellException(ResultEnums.ORDER_AMOUNT_ERROR);
        }
        orderService.payed(orderDTO);
        return payResponse;
    }

    @Override
    public RefundRequest refund(OrderDTO orderDTO) {
        //退款
        RefundRequest refundRequest=new RefundRequest();
        //返回 订单编号，金额，类型
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_MWEB); //微信H5
        log.info("微信退款，refundRequest={}",JsonUtil.ToJson(refundRequest));
        bestPayService.refund(refundRequest);
        return  refundRequest;
    }
}
